﻿namespace Calendar
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Globalization;

    public class Office12Renderer : AbstractRenderer
    {
        private Font baseFont;

        protected override void Dispose(bool mainThread)
        {
            base.Dispose(mainThread);
            if (this.baseFont != null)
            {
                this.baseFont.Dispose();
            }
        }

        public override void DrawAppointment(Graphics g, Rectangle rect, Appointment appointment, bool isSelected, Rectangle gripRect)
        {
            if (appointment == null)
            {
                throw new ArgumentNullException("appointment");
            }
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if ((rect.Width != 0) && (rect.Height != 0))
            {
                using (StringFormat format = new StringFormat())
                {
                    LinearGradientBrush brush2;
                    Pen pen;
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Near;
                    Color baseColor = AbstractRenderer.InterpolateColors(appointment.Color, Color.White, 0.4f);
                    Color color2 = AbstractRenderer.InterpolateColors(appointment.Color, Color.FromArgb(0xbf, 210, 0xea), 0.7f);
					//if (appointment.Locked)
					//{
					//    using (Brush brush = new HatchBrush(HatchStyle.LargeConfetti, Color.Blue, appointment.Color))
					//    {
					//        g.FillRectangle(brush, rect);
					//    }
					//    baseColor = Color.FromArgb(230, baseColor);
					//    color2 = Color.FromArgb(180, color2);
					//    new GraphicsPath().AddRectangle(rect);
					//}
					using (brush2 = new LinearGradientBrush(rect, baseColor, baseColor, LinearGradientMode.Horizontal))
                    {
                        g.FillRectangle(brush2, rect);
                    }
                    if (isSelected)
                    {
                        Rectangle rectangle = rect;
                        using (pen = new Pen(appointment.BorderColor, 3f))
                        {
                            g.DrawRectangle(pen, rect);
                        }
                        rectangle.Inflate(2, 2);
                        using (pen = new Pen(SystemColors.WindowFrame, 1f))
                        {
                            g.DrawRectangle(pen, rectangle);
                        }
                        rectangle.Inflate(-4, -4);
                        using (pen = new Pen(SystemColors.WindowFrame, 1f))
                        {
                            g.DrawRectangle(pen, rectangle);
                        }
                    }
                    else
                    {
                        gripRect.Width++;
                        baseColor = AbstractRenderer.InterpolateColors(appointment.BorderColor, appointment.Color, 0.2f);
                        color2 = AbstractRenderer.InterpolateColors(appointment.BorderColor, Color.White, 0.6f);
                        using (brush2 = new LinearGradientBrush(rect, baseColor, color2, LinearGradientMode.Vertical))
                        {
                            g.FillRectangle(brush2, gripRect);
                        }
                        if (appointment.DrawBorder)
                        {
                            using (pen = new Pen(SystemColors.WindowFrame, 1f))
                            {
                                g.DrawRectangle(pen, rect);
                            }
                        }
                        int num = rect.X + 6;
                        int num2 = rect.Right + 1;
                        int num3 = rect.Y + 1;
                        int num4 = rect.Bottom + 1;
                        for (int i = 0; i < 5; i++)
                        {
                            using (Pen pen2 = new Pen(Color.FromArgb(70 - (12 * i), Color.Black)))
                            {
                                g.DrawLine(pen2, (int) (num + i), (int) (num4 + i), (int) ((num2 + i) - 1), (int) (num4 + i));
                                g.DrawLine(pen2, (int) (num2 + i), (int) (num3 + i), (int) (num2 + i), (int) (num4 + i));
                            }
                        }
                    }
                    rect.X += gripRect.Width;
                    rect.Width -= 6;
                    g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                    g.DrawString(appointment.Title, this.BaseFont, SystemBrushes.WindowText, rect, format);
                    g.TextRenderingHint = TextRenderingHint.SystemDefault;
                }
            }
        }

        public override void DrawDayBackground(Graphics g, Rectangle rect)
        {
            using (SolidBrush brush = new SolidBrush(Color.FromArgb(230, 0xed, 0xf7)))
            {
                g.FillRectangle(brush, rect);
            }
        }

        public override void DrawDayHeader(Graphics g, Rectangle rect, DateTime date)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (StringFormat format = new StringFormat())
            {
                format.Alignment = StringAlignment.Center;
                format.FormatFlags = StringFormatFlags.NoWrap;
                format.LineAlignment = StringAlignment.Center;
                using (StringFormat format2 = new StringFormat())
                {
                    Pen pen;
                    LinearGradientBrush brush2;
                    format2.Alignment = StringAlignment.Near;
                    format2.FormatFlags = StringFormatFlags.NoWrap;
                    format2.LineAlignment = StringAlignment.Center;
                    using (SolidBrush brush = new SolidBrush(this.BackColor))
                    {
                        g.FillRectangle(brush, rect);
                    }
                    using (pen = new Pen(Color.FromArgb(0xcd, 0xdb, 0xee)))
                    {
                        g.DrawLine(pen, rect.Left, rect.Top + (rect.Height / 2), rect.Right, rect.Top + (rect.Height / 2));
                    }
                    using (pen = new Pen(Color.FromArgb(0x8d, 0xae, 0xd9)))
                    {
                        g.DrawRectangle(pen, rect);
                    }
                    rect.X++;
                    rect.Width--;
                    using (pen = new Pen(Color.FromArgb(0x8d, 0xae, 0xd9)))
                    {
                        g.DrawRectangle(pen, rect);
                    }
                    Rectangle rectangle = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, (rect.Height / 2) - 1);
                    Rectangle rectangle2 = new Rectangle(rect.Left + 1, (rect.Top + (rect.Height / 2)) + 1, rect.Width - 1, (rect.Height / 2) - 1);
                    using (brush2 = new LinearGradientBrush(rectangle, Color.FromArgb(0xe4, 0xec, 0xf6), Color.FromArgb(0xd6, 0xe2, 0xf1), LinearGradientMode.Vertical))
                    {
                        g.FillRectangle(brush2, rectangle);
                    }
                    using (brush2 = new LinearGradientBrush(rectangle2, Color.FromArgb(0xc2, 0xd4, 0xeb), Color.FromArgb(0xd0, 0xde, 0xef), LinearGradientMode.Vertical))
                    {
                        g.FillRectangle(brush2, rectangle2);
                    }
                    if (date.Date.Equals(DateTime.Now.Date))
                    {
                        rectangle.Inflate((-rectangle.Width / 4) + 1, 1);
                        rectangle.Offset((rect.Left - rectangle.Left) + 1, 1);
                        rectangle.Inflate(1, 0);
                        using (brush2 = new LinearGradientBrush(rectangle, Color.FromArgb(0xf7, 0xcf, 0x72), Color.FromArgb(0xfb, 230, 0x94), LinearGradientMode.Horizontal))
                        {
                            rectangle.Inflate(-1, 0);
                            g.FillRectangle(brush2, rectangle);
                        }
                        rectangle.Offset(rect.Right - rectangle.Right, 0);
                        rectangle.Inflate(1, 0);
                        using (brush2 = new LinearGradientBrush(rectangle, Color.FromArgb(0xfb, 230, 0x94), Color.FromArgb(0xf7, 0xcf, 0x72), LinearGradientMode.Horizontal))
                        {
                            rectangle.Inflate(-1, 0);
                            g.FillRectangle(brush2, rectangle);
                        }
                        using (pen = new Pen(Color.FromArgb(0x80, 240, 0x9a, 30)))
                        {
                            g.DrawLine(pen, rect.Left, rectangle.Bottom - 1, rect.Right, rectangle.Bottom - 1);
                        }
                        rectangle.Inflate(0, -1);
                        rectangle.Offset(0, rectangle.Height + 1);
                        using (brush2 = new LinearGradientBrush(rectangle, Color.FromArgb(240, 0x9d, 0x21), Color.FromArgb(250, 0xe2, 0x8e), LinearGradientMode.BackwardDiagonal))
                        {
                            g.FillRectangle(brush2, rectangle);
                        }
                        rectangle.Offset((rect.Left - rectangle.Left) + 1, 0);
                        using (brush2 = new LinearGradientBrush(rectangle, Color.FromArgb(240, 0x9d, 0x21), Color.FromArgb(250, 0xe2, 0x8e), LinearGradientMode.ForwardDiagonal))
                        {
                            g.FillRectangle(brush2, rectangle);
                        }
                        using (pen = new Pen(Color.FromArgb(0xee, 0x93, 0x11)))
                        {
                            g.DrawRectangle(pen, rect);
                        }
                    }
                    g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                    string dayName = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek);
                    if (rect.Width < 0x69)
                    {
                        dayName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedDayName(date.DayOfWeek);
                    }
                    rect.Offset(2, 1);
                    using (Font font = new Font("Segoe UI", 8f))
                    {
                        g.DrawString(dayName, font, SystemBrushes.WindowText, rect, format);
                    }
                    rect.Offset(-2, -1);
                    using (Font font2 = new Font("Segoe UI", 9f, FontStyle.Bold))
                    {
                        g.DrawString(date.ToString(" d"), font2, SystemBrushes.WindowText, rect, format2);
                    }
                }
            }
        }

        public override void DrawHourLabel(Graphics g, Rectangle rect, int hour, bool ampm)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (SolidBrush brush = new SolidBrush(this.TextColor))
            {
                string str;
                if (ampm)
                {
                    if (hour < 12)
                    {
                        str = "AM";
                    }
                    else
                    {
                        str = "PM";
                    }
                    if (hour != 12)
                    {
                        hour = hour % 12;
                    }
                }
                else
                {
                    str = "00";
                }
                g.DrawString(hour.ToString("##00", CultureInfo.InvariantCulture), this.HourFont, brush, rect);
                rect.X += 0x1b;
                g.DrawString(str, this.MinuteFont, brush, rect);
            }
        }

        public override void DrawMinuteLine(Graphics g, Rectangle rect)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            using (Pen pen = new Pen(AbstractRenderer.InterpolateColors(this.TextColor, Color.White, 0.5f)))
            {
                g.DrawLine(pen, rect.Left, rect.Y, rect.Width, rect.Y);
            }
        }

        public override Color BackColor
        {
            get
            {
                return Color.FromArgb(0xd5, 0xe4, 0xf2);
            }
        }

        public override Font BaseFont
        {
            get
            {
                if (this.baseFont == null)
                {
                    this.baseFont = new Font("Segoe UI", 8f, FontStyle.Regular);
                }
                return this.baseFont;
            }
        }

        public override Color HalfHourSeperatorColor
        {
            get
            {
                return Color.FromArgb(0xa5, 0xbf, 0xe1);
            }
        }

        public override Color HourColor
        {
            get
            {
                return Color.FromArgb(230, 0xed, 0xf7);
            }
        }

        public override Color HourSeperatorColor
        {
            get
            {
                return Color.FromArgb(0xd5, 0xd7, 0xf1);
            }
        }

        public override Color SelectionColor
        {
            get
            {
                return Color.FromArgb(0x29, 0x4c, 0x7a);
            }
        }

        public Color TextColor
        {
            get
            {
                return Color.FromArgb(0x65, 0x93, 0xcf);
            }
        }

        public override Color WorkingHourColor
        {
            get
            {
                return Color.FromArgb(0xff, 0xff, 0xff);
            }
        }
    }
}